
	//访问用户媒体设备的兼容方法
    function getUserMedia(constraints, success, error) {
      if (navigator.mediaDevices.getUserMedia) {
        //最新的标准API
        navigator.mediaDevices.getUserMedia(constraints).then(success);
      } else if (navigator.webkitGetUserMedia) {
        //webkit核心浏览器
        navigator.webkitGetUserMedia(constraints,success, error)
      } else if (navigator.mozGetUserMedia) {
        //firfox浏览器
        navigator.mozGetUserMedia(constraints, success, error);
      } else if (navigator.getUserMedia) {
        //旧版API
        navigator.getUserMedia(constraints, success, error);
      }
    }

    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let context = canvas.getContext('2d');

    function success(stream) {
      //兼容webkit核心浏览器
      let CompatibleURL = window.URL || window.webkitURL;
      //将视频流设置为video元素的源
      console.log(stream);

      //video.src = CompatibleURL.createObjectURL(stream);
      video.srcObject = stream;
      video.play();
    }

    function error(error) {
      console.log('访问用户媒体设备失败${error.name}, ${error.message}');
    }

    
    function clearCanvas()
    {  
        var c=document.getElementById("canvas");  
        var cxt=c.getContext("2d");  
        c.height=c.height;  
    }  
    
    function camerastart() {
	    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
	      //调用用户媒体设备, 访问摄像头
	      getUserMedia({video : {width: 640, height: 480}}, success, error);
	    } else {
	      alert('不支持访问用户媒体');
	    }
    }
    function capture(sid){
      //context.drawImage(video, 100, 40, 440, 440, 0, 0, 320, 240); 拉伸版
      context.drawImage(video, 0, 0, 640, 480, 0, 0, 320, 240);
      var dataURL = canvas.toDataURL('images/png');
      var imageDataB64 = dataURL.substring(22);
      $.ajax(
    	{
      		type:"post",
      		url:"img/saveimg.action",
      		dataType:"json",
      		data:{
      			image: imageDataB64,
				sid : sid
      		},success:function(result){
      			layer.msg(result.msg);
      		}
      	});
    }
    
    function train() {
    	$.ajax(
		{
			type:"post",
			url:"img/train.action",
			dataType:"json",
			success:function(result){
				layer.msg(result.msg);
			}
		});
    }